// RegistryFacade.cpp: implementation of the CRegistryFacade class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "RegistryFacade.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CRegistryFacade::CRegistryFacade(const char* szKeyName)
{
	LONG res = m_registry.Create(HKEY_LOCAL_MACHINE, szKeyName);
	if (ERROR_SUCCESS != res)
		OutputDebugString("REGISTRY FEJL!!!\n");
}

CRegistryFacade::~CRegistryFacade()
{
	m_registry.Close();
}

void CRegistryFacade::setKeyToRegistry(const char* name, const char* value)
{	
	m_registry.SetMultiStringValue(name, value);
}

void CRegistryFacade::getKeyFromRegistry(const char* name, char **value)
{
	LONG ret = ERROR_SUCCESS;
	unsigned long lngValue = 0;
	DWORD count = 255;
	char szRetval[255];
  memset(szRetval, 0, 255);

  char * szName = const_cast<char*>(name);
  
	ret = m_registry.QueryMultiStringValue(szName, szRetval, &count);
	if (ERROR_SUCCESS != ret) {
		count = 0;
    OutputDebugString("REGISTRY FEJL!!!\n");
	}

	if (0 != count) {
		*value = (char*)malloc(count +1);
		memset(*value, 0, count +1);
		strncpy(*value, szRetval, count);
	}
}

void CRegistryFacade::getKeyFromRegistry(const char* name, long *value)
{
	DWORD dwType = 256, bufSize = 256;
	char retval[256];
  memset(retval, 0, 256);

	LONG ret = RegQueryValueEx(m_registry.m_hKey, name, 0, &dwType, (unsigned char *)retval, &bufSize);
	if (ERROR_SUCCESS != ret) {
		OutputDebugString("REGISTRY FEJL!!!\n");
	}
  *value = atoi(retval);
}

void CRegistryFacade::getKeyFromRegistry(const char *name, char **value, const char *defValue)
{
  getKeyFromRegistry(name, value);
  if (NULL == *value) {
    setKeyToRegistry(name, defValue);
    getKeyFromRegistry(name, value);
  }  
}